<?php
/**
 * @desc 私人订制数据层
 * @author kevin.wang
 * @date 2015-01-01 12:00:00
 */
class Ptravel extends Base
{
	//数据表
	protected $table = 'pert_person_travel';
	protected $gtable = 'pert_goods';
	//私人订制列表
	public function getPtravelList(array $con)  
    {
		 $table = $this->table;
		 $gtable = $this->gtable;
		 $filed = "a.`person_travel_id`,a.`goods_id`,a.`ptravel_keywords`,a.`transport`,a.`dinner`,a.`intro`,a.`ptravel_url`,a.`is_open`,a.`is_show`,a.`sort`,a.`author`,a.`raw_add_time`,b.`goods_name`";
		 $sql = "select {$filed} from `{$table}` as a,`{$gtable}` as b where a.`goods_id` = b.`goods_id` and a.`delete_flag` = 0";
		 $where = "";
		 if(!empty($con['ptravel_keywords'])){
			 $where .= " and a.`ptravel_keywords` like '%{$con['ptravel_keywords']}%'";
		 }
		 $limit = " order by a.`sort` desc, a.`raw_add_time` desc limit ".(($con['current'] - 1)*$con['numPerPage']).", ".$con['numPerPage'];
		 $return = $this->select_table($sql.$where.$limit, array());
		 $sql = "select count(a.`person_travel_id`) as num from `{$table}` as a,`{$gtable}` as b where a.`goods_id` = b.`goods_id` and a.`delete_flag` = 0";
		 $return[] = $this->select_table($sql.$where, array(), Base::QUERY_COLUMN);
		 return $return;
    }
	//删除私人订制
	public function deletePtravel($ids){
		$table = $this->table;
		$sql = "update `{$table}` set `delete_flag` = 1 where `person_travel_id` in ({$ids})";
		return $this->update_table($sql, array());
	}
	//写入私人订制
	public function insertPtravel(array $data){
		if(empty($data)){
			return false;
		}
		return self::insert_table($data, $this->table);
	}
	//获取私人订制
	public function getPtravel($id){
		$table = $this->table;
		$filed = "`person_travel_id`,`goods_id`,`ptravel_keywords`,`transport`,`dinner`,`intro`,`ptravel_url`,`is_open`,`is_show`,`sort`,`author`,`raw_add_time`";
		$sql = "select {$filed} from `{$table}` where `person_travel_id` = :person_travel_id and `delete_flag` = 0";
		return $this->select_table($sql, array(':person_travel_id'=>$id), Base::QUERY_RAW);
	}
	//私人订制修改
	public function editPtravel($id, array $data){
		if(empty($id) || empty($data)){
			return false;
		}
		$table = $this->table;
		$sql = "update `{$table}` set ";
		$binds = array();
		foreach($data as $key=>$value){
			$sql .= "`{$key}` = :{$key},";
			$binds[":{$key}"] = $value;
		}
		$sql = trim($sql, ',');
		$sql .= " where `person_travel_id` = :person_travel_id";
		$binds[':person_travel_id'] = $id;
		return $this->update_table($sql, $binds);
	}
}
